package calao.client.ui;

import java.util.Comparator;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Widgets 
{

	public static TextBox newTextBox(int length)
	{
		TextBox box = new TextBox();
		box.setVisibleLength(length);
		return box;
	}

	public static TextBox newTextBox()
	{
		return newTextBox(40);
	}
	
	public static TextBox newSmallTextBox()
	{
		return newTextBox(20);
	}

	public static TextBox newTinyTextBox()
	{
		return newTextBox(10);
	}

	public static TextBox newLargeTextBox()
	{
		return newTextBox(80);
	}
	
	public static void insert(VerticalPanel panel, Widget widget, Comparator comparator)
	{
		int start = 0;
		int end = panel.getWidgetCount();
		int index = 0;
		while (start<end) {
			index = (start+end)/2;
			int comp = comparator.compare(widget, panel.getWidget(index));
			if (comp==0) start = end = index;
			else if (comp<0) end = index;
			else start = index+1;
		}
		panel.insert(widget, end);		
	}
	
	public static Widget newVerticalSpacer(int height)
	{
		FlowPanel spacer = new FlowPanel();
		spacer.setWidth("100%");
		Elements.setHeight(spacer.getElement(), height);
		return spacer;
	}

	public static Widget newVerticalSpacer()
	{
		return newVerticalSpacer(SpacerPanel.DEFAULT_SPACE_SIZE);
	}

}
